<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- 表单区域 -->
      <el-form ref="form" :model="form" :rules="rules" class="login-form">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="isAgree">
          <el-checkbox v-model="form.isAgree" label="我已阅读并同意用户协议和隐私条款" name="type"></el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button @click.prevent="hLogin" class="login-btn" type="primary"  :loading="isLoading">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        mobile: '13911111111', // 手机号
        code: '246810', // 密码
        isAgree: false
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入合法的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入6位数的验证码', trigger: 'blur' }
        ],
        isAgree: [
          { // 自定义校验规则
            validator: function (rule, value, callback) {
              if (value) { // 选中
                callback()
              } else { // 用户不同意
                callback(new Error('你必须要同意勾选！'))
              }
            },
            trigger: 'change'
          }
        ]
      },
      isLoading: false
    }
  },
  methods: {
    hLogin () {
      this.$refs.form.validate(async valid => {
        // 验证通过
        if (valid) {
          try {
            // 加载中
            this.isLoading = true
            const { data: res } = await login({
              mobile: this.form.mobile,
              code: this.form.code
            })
            // 把返回值中的token保存到本地
            localStorage.setItem('TOKEN', res.data.token)
            console.log(res)
            this.$message.success('登陆成功')
            this.isLoading = false
            // 跳转首页
            this.$router.push('/')
          } catch (error) {
            console.log(error)
            this.$message.error('登陆失败')
            this.isLoading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  // flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("@/assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("http://huangjinlin.gitee.io/hm-toutiao-pc-dev/img/logo_index.e0892259.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
